﻿/*
 * @Autor: PK
 * @Date: 2021/12/22 10:08:SS
 */

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShkSimulation.core.optimization.variable {

	public class DecisionVariableVector<T> : IEnumerable<DecisionVariable<T>> {
		
		private DecisionVariable<T>[] data;
		
		public DecisionVariableVector(Dictionary<string, T> variableValues, T lowerBound, T upperBound) {
			this.data = new DecisionVariable<T>[variableValues.Count];
			int index = 0;
			foreach (var (key, value) in variableValues) {
				data[index] = new ConstinousVariable<T>(key, value, lowerBound, upperBound);
				index++;
			}
			NameKeys = variableValues.Keys.OrderBy(each => each).ToList();
		}
		
		public List<string> NameKeys { get; set; }

		public DecisionVariable<T>[] GetVariables() => data;
		
		public T[] GetVariableValues() => data.Select(each => each.value).ToArray();

		public DecisionVariable<T> this[int index] => data[index];

		public int Length => data.Length;

		public IEnumerator<DecisionVariable<T>> GetEnumerator() => data.Cast<DecisionVariable<T>>().GetEnumerator();

		public override string ToString() {
			StringBuilder builder = new StringBuilder();
			builder.Append("\r\n");
			foreach (var decisionVariable in data) {
				builder.AppendFormat("{0}: {1}\r\n", decisionVariable.Name, decisionVariable.value);
			}
			builder.Remove(builder.Length - 2, 2);
			return builder.ToString();
		}

		IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
	}
}